package com.ruoyi.web.wx.utils;

import com.riversoft.weixin.pay.base.PaySetting;
import com.riversoft.weixin.pay.redpack.RedPacks;
import com.riversoft.weixin.pay.redpack.bean.RedPackResult;
import com.ruoyi.web.wx.entity.*;
import com.ruoyi.web.wx.service.ICashOutService;
import com.ruoyi.web.wx.service.IEnvirBonusTotalService;
import com.ruoyi.web.wx.service.IWxUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component("HongBaoTask")
public class HongBaoTask
{
    @Autowired
    private IEnvirBonusTotalService envirBonusTotalService;
    @Autowired
    private ICashOutService cashOutService;
    @Autowired
    private IWxUserService wxUserService;
    @Autowired
    private AppInfo appInfo;

    public void updateRedPackStatus(){

        Calendar cal = Calendar.getInstance();

        String endTime = DateFormatUtils.format(cal.getTime(), "yyyy-MM-dd HH:mm:ss");
        cal.add(Calendar.HOUR_OF_DAY,-25);
        String startTime = DateFormatUtils.format(cal.getTime(), "yyyy-MM-dd HH:mm:ss");
        List<CashOut >cashOuts=  cashOutService.selectCashOutList(startTime,endTime);
        for(int i=0;i<cashOuts.size();i++){
            CashOut cashOut = cashOuts.get(i);
            String orderId = cashOut.getOrderNum();
            PaySetting paySetting = new PaySetting();
            paySetting.setAppId(appInfo.getOfficialAccountAppid());
            paySetting.setMchId(appInfo.getPayMchid());
            paySetting.setKey(appInfo.getPayApikey());
            paySetting.setCertPath(appInfo.getPayCertPath());
            paySetting.setCertPassword(appInfo.getPayCertPassword());
            PaySetting.setDefault(paySetting);
            RedPacks redPacks = RedPacks.defaultRedPacks();
            RedPackResult query = redPacks.query(orderId);
            String status = query.getStatus();
            if(StringUtils.isNotEmpty(status)&&!status.equals(WithdrawalStatus.WX_SENT)){
                if(status.equals(WithdrawalStatus.WX_RECEIVED.getInfo())){
                    cashOut.setStatus("2");

                }else if(status.equals(WithdrawalStatus.WX_REFUND.getInfo())){
                    cashOut.setStatus("3");
                    //给用户退款
                    String wxUserId = cashOut.getWxUserId();
                    Double cashWithdrawal = cashOut.getCashWithdrawal();
                    WxUser wxUser = wxUserService.selectWxUserById(wxUserId);
                    if(wxUser!=null){
                        double totalMoney = wxUser.getTotalMoney();
                        totalMoney = totalMoney +cashWithdrawal;
                        wxUser.setTotalMoney(totalMoney);
                        wxUserService.updateWxUser(wxUser);
                    }
                    //envir_bonus_total 表增加记录
                    EnvirBonusTotal envirBonusTotal = new EnvirBonusTotal();
                    envirBonusTotal.setId(CommonUtil.getUUID());
                    envirBonusTotal.setWxUserId(cashOut.getWxUserId());
                    envirBonusTotal.setCreateTime(new Date());
                    envirBonusTotal.setSource("99");
                    envirBonusTotal.setType("3");
                    envirBonusTotal.setMoney(cashWithdrawal);
                    envirBonusTotal.setIntegral("0");
                    envirBonusTotalService.insertEnvirBonusTotal(envirBonusTotal);
                }else if(status.equals(WithdrawalStatus.WX_FAILED.getInfo())){
                    //给用户退款
                    String wxUserId = cashOut.getWxUserId();
                    Double cashWithdrawal = cashOut.getCashWithdrawal();
                    WxUser wxUser = wxUserService.selectWxUserById(wxUserId);
                    if(wxUser!=null){
                        double totalMoney = wxUser.getTotalMoney();
                        totalMoney = totalMoney +cashWithdrawal;
                        wxUser.setTotalMoney(totalMoney);
                        wxUserService.updateWxUser(wxUser);
                    }
                    //envir_bonus_total 表增加记录
                    EnvirBonusTotal envirBonusTotal = new EnvirBonusTotal();
                    envirBonusTotal.setId(CommonUtil.getUUID());
                    envirBonusTotal.setWxUserId(cashOut.getWxUserId());
                    envirBonusTotal.setCreateTime(new Date());
                    envirBonusTotal.setSource("99");
                    envirBonusTotal.setType("3");
                    envirBonusTotal.setMoney(cashWithdrawal);
                    envirBonusTotal.setIntegral("0");
                    envirBonusTotalService.insertEnvirBonusTotal(envirBonusTotal);
                }else if(status.equals(WithdrawalStatus.WX_SENT.getInfo())){
                    //告知用户尽快领取红包
                }
                cashOutService.updateCashOut(cashOut);
            }
        }
    }

/*
    public void clearUserWithDrawLimit(){
        tsUserService.clearUserWithDrawLimit();

    }*/
}